package com.client;

import java.util.ArrayList;
import java.util.Vector;

import com.wink.dt.*;
import com.wink.event.*;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;

public class Client {
	private String endPoint = "http://localhost:8080/ReachOut/rest/community";

	public Client() {
	}

	public Client(String url) {
		endPoint = url;
	}
	
	/*public void demolistSubjects() {		
		System.out.println("\n **Client --> In demolistSubjects");
		try {
			String url = endPoint + "/DT/subject";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			@SuppressWarnings("unchecked")
			SubjectList list = resource.accept(MediaType.APPLICATION_XML).get(SubjectList.class);
			System.out.println("Subjects:");
			ArrayList<Subject> eList = list.getList();
			for (Subject s : eList)
				System.out.println("Subject parent: " + s.getParent() 
						+ " name " + s.getName()
						+ " description " + s.getDescription());
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateSubjectEntry() {		
		System.out.println("\n **Client --> In demoCreateSubjectEntry");
		try {
			String url = endPoint + "/DT/subjectentry";
			System.out.println("\n **endPoint: " + url);
			Comment s = new Comment();
			s.setName("SubjectEntry1");
			s.setDescription("Test creating a SubjectEntry, SubjectEntry 1 from a client");
			s.setParent("News:Subject1");

			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			Comment created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(Comment.class, s);
			System.out.println("Created SubjectEntry: " + created.toString());
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateSubject() {		
		System.out.println("\n **Client --> In demoCreateSubject");
		try {
			String url = endPoint + "/DT/subject";
			System.out.println("\n **endPoint: " + url);
			Subject s = new Subject();
			s.setName("Subject1");
			s.setDescription("Test creating a Subject, Subject1 from a client");
			s.setParent("News");

			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			Subject created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(Subject.class, s);
			System.out.println("Created subject: " + created.toString());
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void demoListTopics() {		
		System.out.println("\n **Client --> In demoListTopics");
		try {
			String url = endPoint + "/DT/topics";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			TopicList list = resource.accept(MediaType.APPLICATION_XML).get(TopicList.class);
			System.out.println("Topics:");
			for (Topic s : list.getList())
			{
				System.out.println("Topic : " + s.getName() + " has " +  " entries");
				ArrayList<Subject> subjects = s.getSubjectList();
				for (Subject ss : subjects)
				{
					System.out.println("Subject : " + ss.getName() + " has " + ss.getDescription());
					ArrayList<Comment> subjectEntries = ss.getSubjectEntryList();
					for (Comment se : subjectEntries)
						System.out.println("SubjectEntry : " + se.getEntry() );
				}
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	*/
	public void demoListEvents() {	
		System.out.println("\n **List demoListEvents: ");
		try {
			String url = endPoint + "/events";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			EventList list = resource.accept(MediaType.APPLICATION_XML).get(EventList.class);
			System.out.println("Events:");
			ArrayList<Event> eList = list.getList();
			for (Event s : eList)
				System.out.println("Event Owner: " + s.getOwner() + " has " + s.getDescription());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoCreateEvent() {		
		System.out.println("\n **List demoCreateEvent: ");
		try {
			String url = endPoint + "/event";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			@SuppressWarnings("unchecked")
			Event e = resource.accept(MediaType.APPLICATION_XML).get(Event.class);
			System.out.println("Event :");
			
			System.out.println("Event Owner: " + e.getOwner() + " has " + e.getDescription());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void demoServices() {
		System.out.println("\n **List demoServices: ");
		try {
			String url = endPoint + "/services";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			// we can get the return as a string or converted to an object
			// String response =
			// resource.accept(MediaType.APPLICATION_XML).get(String.class);
			@SuppressWarnings("unchecked")
			//Event e = resource.accept(MediaType.APPLICATION_XML).get(Event.class);
			String services = resource.accept(MediaType.APPLICATION_XML).get(String.class);
			System.out.println("Services :" + services);			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	

	public void demoGetDTAsHTML(String blogId) {
		System.out.println("\n** Get DT (html): retrieve a DT as html:");
		try {
			int id = 1;
			String url = endPoint + "/dt/" + blogId;
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);

			// example of retrieving results as a string of HTML
			String response = resource.accept(MediaType.TEXT_HTML).get(String.class);

			System.out.println("dt (html): " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
		public void demoDT() {
			Topic topic = new Topic();
			topic.setTopicTitle("Topic1");

			String url = endPoint + "/DT";
			System.out.println("\n **endPoint: " + url);
			RestClient client = new RestClient();
			Resource resource = client.resource(url);
			Topic created = resource.contentType(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(Topic.class, topic);
				
			//createSubject(Subject subject);			
			
			//createSubjectEntry(Subject subject, SubjectEntry subjectEntry);
		}
		

	public static void main(String[] args) {
		Client bc = new Client("http://localhost:8080/ReachOut/rest/community");

		bc.demoServices();		

		bc.demoListEvents();
		bc.demoCreateEvent();
		
		//bc.demoDT();
		
		/*bc.demoListTopics();
		
		bc.demoCreateSubject();
		
		bc.demoCreateSubjectEntry();
		
		bc.demolistSubjects();*/
		
			//bc.demoGetBlog("6"); //exception
		//bc.demoGetBlog("7"); //exception
		
		//bc.demoGetDTAsHTML("1");
		//bc.demoGetDTAsHTML("4");
		
		//bc.demoGetDTEntries("3");
		
		
	
		
		
	}
}
